/*
 * $Id: MainWindow.java,v 1.3 2001/10/07 18:53:19 jppietsch Exp $
 * Copyright (C) 2001 Jan-Peter Pietsch
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package de.jppietsch.gui;


import de.jppietsch.error.Precondition;
import de.jppietsch.util.RscBundle;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;


/**
 * Convenient base class for main windows of applications.
 *
 * @author Jan-Peter Pietsch
 * @version $Revision: 1.3 $
 */
public abstract class MainWindow extends JDialog {

    // constants

    /**
     * Resource key for window title.
     */
    public static final String TITLE = "mainWindow.title";

    /**
     * Resource key for file menu.
     */
    public static final String FILE = "mainWindow.fileMenu";

    /**
     * Resource key prefix for exit action.
     */
    public static final String EXIT = "mainWindow.exitAction";

    // fields

    /**
     * The menu bar of this window.
     */
    private final JMenuBar menuBar = new JMenuBar();

    /**
     * The tool bar.
     */
    private final JToolBar toolBar = new JToolBar();

    // constructors

    /**
     * Constructs this window with its menu bar and exit method.
     *
     * @param aResourceBundleClass contains this window's title.
     */
    protected MainWindow(Frame owner, Class aResourceBundleClass) {
        super(owner, RscBundle.getBundle(aResourceBundleClass).getString(TITLE));

        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        setJMenuBar(menuBar);
        menuBar.add(createFileMenu(RscBundle.getBundle(aResourceBundleClass)));

        getContentPane().add(toolBar, BorderLayout.NORTH);
    }

    // overwritten methods

    /**
     * Exits the application on window closing events.
     *
     * @param anEvent processed if closing.
     */
    protected void processWindowEvent(WindowEvent anEvent) {
        super.processWindowEvent(anEvent);
        if (anEvent.getID() == WindowEvent.WINDOW_CLOSING) {
            exit();
        }
    }

    // protected methods

    /**
     * Overwrite this method to change the exit behaviour of your application.
     * This default implementation just calls <code>System.exit</code>.
     */
    protected void exit() {
//        System.exit(0);
    }

    /**
     * Adds a new menu to the menu bar.
     *
     * @param aResourceBundleClass contains this window's title.
     * @param aPrefix              ressource key prefix.
     * @return A new menu which is already added to the menu bar.
     */
    protected JMenu addMenu(Class aResourceBundleClass, String aPrefix) {
        Precondition.argumentNotNull("aResourceBundleClass",
                aResourceBundleClass);
        Precondition.argumentStringNotEmpty("aPrefix", aPrefix);
        JMenu menu = new RscMenu(RscBundle.getBundle(aResourceBundleClass),
                aPrefix);

        menuBar.add(menu);

        return menu;
    }

    /**
     * Adds an action to the toolbar.
     *
     * @param anAction to be used as a tool.
     */
    protected void addTool(Action anAction) {
        toolBar.add(anAction);
    }

    /**
     * Creates the file menu.
     *
     * @return A new menu.
     */
    protected JMenu createFileMenu(RscBundle aBundle) {
        JMenu menu = new RscMenu(aBundle, FILE);

        menu.add(new ExitAction(aBundle));

        return menu;
    }

    // inner classes

    /**
     * Default action to exit the application.
     * It simply calls {@link MainWindow#exit()}.
     *
     * @author Jan-Peter Pietsch
     * @version $Revision: 1.3 $
     */
    public class ExitAction extends RscAction {

        /**
         * Localizes this action with the outer class's bundle and the key
         * prefix {@link MainWindow#EXIT}.
         *
         * @throws java.util.MissingResourceException
         *          Some resources are missing (like
         *          <code>EXIT+NAME</code>).
         */
        public ExitAction(RscBundle aBundle) {
            super(aBundle, EXIT);
        }

        /**
         * Calls {@link MainWindow#exit()}.
         *
         * @param anEvent is ignored (may be <code>null</code>).
         */
        public void actionPerformed(ActionEvent anEvent) {
            exit();
        }

    }

}